package com.aaa.cs.service.impl;

import com.aaa.cs.dao.ViolationRecordDao;
import com.aaa.cs.entity.ViolationRecord;
import com.aaa.cs.service.ViolationRecordService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cs.dao.ViolationRecordDao;
import com.aaa.cs.entity.ViolationRecord;
import com.aaa.cs.service.ViolationRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ViolationRecord)表服务实现类
 *
 * @author makejava
 * @since 2020-11-19 09:33:24
 */
@Service("violationRecordService")
public class ViolationRecordServiceImpl implements ViolationRecordService {
    @Resource
    private ViolationRecordDao violationRecordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ViolationRecord queryById(Integer id) {
        return this.violationRecordDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ViolationRecord> queryAllByLimit(int offset, int limit) {
        return this.violationRecordDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param violationRecord 实例对象
     * @return 实例对象
     */
    @Override
    public ViolationRecord insert(ViolationRecord violationRecord) {
        violationRecordDao.insert(violationRecord);
        return violationRecord;
    }

    /**
     * 修改数据
     *
     * @param violationRecord 实例对象
     * @return 实例对象
     */
    @Override
    public ViolationRecord update(ViolationRecord violationRecord) {
        this.violationRecordDao.update(violationRecord);
        return this.queryById(violationRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.violationRecordDao.deleteById(id) > 0;
    }


    @Override
    public Result queryPageByParam(Integer pageNo, Integer pageSize, ViolationRecord violationRecord) {
      if (pageNo==null||pageSize==null){
          throw  new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                  ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
      }
      //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<ViolationRecord>pageInfo=new PageInfo<ViolationRecord>(violationRecordDao.queryAll(violationRecord));
        return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(),pageInfo);
    }

//    @Override
//    public ViolationRecord queryPageByParam(int id) {
//        ViolationRecord violationRecord = new ViolationRecord();
//        violationRecord.setId(id);
//        System.out.println(true);
//        List<ViolationRecord>violationRecords=violationRecordDao.queryAll(violationRecord);
//        if (violationRecords!=null&&violationRecords.size()>0){
//            return violationRecords.get(0);
//        }
//        return null;
//    }
}